<template>
  <div class="container-item-search">
    <div class="group-input">
      <input @click="inputFoucsHandler" class="item-input-search" placeholder="搜索框" type="text" />
      <button @click="searchHandler" class="item-btn-search" />
    </div>
    <transition name="history-panel">
      <ul class="item-history-panel" v-show="showHistory">
        <li class="item-kw">
          <div class="item-line">
            <p>
              <span>历史搜索</span>
            </p>
          </div>
        </li>
        <li class="item-suggest">
          <a href="#/search" target="_blank">asd</a>
          <div @click="cancelHandler" class="item-cancel" />
        </li>
      </ul>
    </transition>
  </div>
</template>

<script>
export default {
    data() {
        return {
            showHistory: false,
        };
    },
    methods: {
        searchHandler() {
            console.log('点击了搜索按钮');
        },
        inputFoucsHandler(evt) {
            evt.stopPropagation();
            this.showHistory = true;
            const closeHistory = () => {
                this.showHistory = false;
                window.removeEventListener('click', closeHistory);
            };
            window.addEventListener('click', closeHistory);
        },
        cancelHandler(evt) {
            evt.stopPropagation();
            console.log('点击了清除历史记录按钮');
        },
    },
};
</script>

<style>
.history-panel-enter-active {
  transition: all 0.1s cubic-bezier(1, 0.8, 0.5, 1);
}
.history-panel-leave-active {
  transition: all 0.1s cubic-bezier(1, 0.5, 0.8, 1);
}
.history-panel-enter, .history-panel-leave-to
/* .history-panel-leave-active for below version 2.1.8 */ {
  transform: translateY(10px);
  opacity: 0;
}
.history-panel-leave-to {
  width: 264px;
  position: absolute;
  left: 2px;
}
</style>

<style lang="less" scoped>
.container-item-search {
  width: 268px;
  padding: 2px;
  border-radius: 6px;
  font-size: 12px;
  .group-input {
    background-color: hsla(0, 0%, 100%, 0.88);
    height: 32px;
    border-radius: 4px;
    transition: background-color 0.2s;
  }
  .item-input-search {
    width: 200px;
    height: 32px;
    padding: 0 12px;
    box-sizing: initial;
    float: left;
    border: 0;
    border-radius: 6px;
    font-size: 12px;
    box-shadow: 0;
    background-color: transparent;
  }
  .item-btn-search {
    display: block;
    position: absolute;
    right: 0;
    width: 48px;
    min-width: 0;
    cursor: pointer;
    height: 32px;
    background: url(//miva-unmcc.oss-cn-beijing.aliyuncs.com/egg-multipart/icons.png) -653px -720px;
    margin: 0;
    padding: 0;
    border: 0;
    &:hover {
      background-position: -718px -720px;
    }
  }
  .group-input:hover {
    background-color: #fff;
  }
  .item-history-panel {
    border: 1px solid #e5e9ef;
    margin-top: 2px;
    background: #fff;
    z-index: 1;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.16);
    padding-bottom: 5px;
    font-size: 12px;
    .item-line {
      border-top: 1px solid #e5e9ef;
      position: relative;
      height: 10px;
      margin: 10px 10px 0;
      p {
        margin-top: -10px;
        text-align: center;
        span {
          display: inline-block;
          padding: 0 10px;
          height: 18px;
          text-align: center;
          cursor: pointer;
          color: #99a2aa;
          background: #fff;
        }
      }
    }
    .item-suggest {
      display: flex;
      cursor: pointer;
      word-wrap: break-word;
      word-break: break-all;
      display: block;
      color: #222;
      position: relative;
      &:hover {
        background-color: #e5e9ef;
      }
      a {
        padding: 6px 10px;
        display: block;
        max-width: 200px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        color: #222;
      }
    }
    .item-cancel {
      position: absolute;
      right: 10px;
      top: 0;
      width: 38px;
      height: 28px;
      background: url(//miva-unmcc.oss-cn-beijing.aliyuncs.com/egg-multipart/icons.png) -461px -530px
        no-repeat;
      &:hover {
        background-position: -525px -530px;
      }
    }
  }
}
</style>
